package com.zgw.his.emr.util.print;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

import com.zgw.his.emr.widget.MedicalPage;

// 打印预览 
public class PrintPreviewDlg extends Dialog {
	protected Object result;
	protected Shell shell;
	private Canvas canvas;
	private List<MedicalPage> pages;
	private Image[] images;
	private Image showImage;
	private int showPageIndex = 0;
	private ScrolledComposite scrolledCom;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public PrintPreviewDlg(Shell parent, int style) {
		super(parent, style);
		setText("打印预览");
	}

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public PrintPreviewDlg(Shell parent, int style, List<MedicalPage> pages) {
		super(parent, style);
		this.pages = pages;
		setText("打印预览");
	}
// 青春痘来
	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.MAX
				| SWT.APPLICATION_MODAL);
		shell.setSize(580, 480);
		shell.setText(getText());
		shell.setLayout(new FormLayout());
		{
			ToolBar toolBar = new ToolBar(shell, SWT.FLAT | SWT.RIGHT);
			{
				FormData formData = new FormData();
				formData.bottom = new FormAttachment(0, 49);
				formData.right = new FormAttachment(100);
				formData.top = new FormAttachment(0);
				formData.left = new FormAttachment(0);
				toolBar.setLayoutData(formData);
			}
			{
				ToolItem toolItem = new ToolItem(toolBar, SWT.NONE);
				toolItem.setText("打印");
				toolItem.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent e) {
						doPrint();
					}
				});
			}
			{
				scrolledCom = new ScrolledComposite(shell, SWT.V_SCROLL
						| SWT.H_SCROLL);
				{
					FormData formData = new FormData();
					formData.bottom = new FormAttachment(100, -10);
					formData.right = new FormAttachment(100, -10);
					formData.top = new FormAttachment(0, 56);
					formData.left = new FormAttachment(0, 10);
					scrolledCom.setLayoutData(formData);
				}
				scrolledCom.setExpandHorizontal(true);
				scrolledCom.setExpandVertical(true);
			}
			{
				canvas = new Canvas(scrolledCom, SWT.NONE);
				scrolledCom.setContent(canvas);
			}
			initializeListener();
			buildPreviewImage();
		}
	}

	protected void doPrint() {
		if (showPageIndex > -1 && showPageIndex < pages.size())
			MedicalPrint.printPage(pages.get(showPageIndex));

	}

	private void initializeListener() {
		canvas.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				if (showPageIndex > -1 && showPageIndex < pages.size()) { // 预览
					MedicalPage page = pages.get(showPageIndex);
					Point printerDPI = Display.getDefault().getDPI(); // 预览情况的打印DPI即屏幕的DPI
					Point screenDPI = Display.getDefault().getDPI();
					MedicalPrint p = new MedicalPrint(page, e.gc, screenDPI,
							printerDPI);
					p.print();
					Point point = page.getControl().computeSize(SWT.DEFAULT,
							SWT.DEFAULT);
					scrolledCom.setMinSize(new Point(point.x, point.y + 20));
				}
			}
		});
	}

	private void buildPreviewImage() {
		if (pages != null && pages.size() > 0) {
			images = new Image[pages.size()];
			int i = 0;
			for (MedicalPage p : pages) {
				Control c = p.getControl();
				GC gc = new GC(c);
				Point cSize = c.getSize();
				Image image = new Image(c.getDisplay(), cSize.x, cSize.y);
				gc.copyArea(image, 0, 0);
				images[i++] = image;
				gc.dispose();
			}
			showImage(0);
		}
	}

	private void showImage(int index) {
		if (images.length > index && index > -1) {
			showImage = images[index];
			canvas.redraw();
			this.showPageIndex = index;
		}
	}
}
